package com.aaa.fpa.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {
    @RequestMapping("/toLoginPage")
    public String toLoginPage()
    {
        //跳转至登录页面
        return "login.jsp";
    }

    /**
     * 登录
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(Model model, HttpServletRequest request, String user, String password)
    {
        //验证登录信息
        if (user.equals("cxk") && password.equals("123456"))
        {
            //验证成功，记录Session信息
            request.getSession().setAttribute("user", user);
            //重定向到首页
            return "redirect:index.jsp";
        }
        else
        {
            model.addAttribute("errorMsg", "账号或密码错误！");
        }
        //跳转至登录页面
        return "login.jsp";
    }

    /**
     * 登出
     */
    @RequestMapping(value = "/logout")
    public String logout(HttpServletRequest request)
    {
        //销毁session对象
        request.getSession().invalidate();

        //重定向到登录页面
        return "redirect:login.jsp";
    }


}
